package com.zzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzy.dao.ClassicDao;
import com.zzy.model.Classic;
import com.zzy.service.ClassicService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("classicService")
@Transactional
public class ClassicServiceImpl extends ServiceImpl<ClassicDao, Classic> implements ClassicService {



    public List<Classic> getListByParam(Classic model){
        List<Classic> list = new ArrayList<>();

        LambdaQueryWrapper<Classic> queryWrapper = new LambdaQueryWrapper<>();
        //字符串具体查询
        //queryWrapper.eq(Classic::getAuthor, "李白");
        //模糊查询
        //queryWrapper.like(Classic::getAuthor, "李");
        //数据大小 num 小于2的 不包括2   想要包括 使用 queryWrapper.le()
        //queryWrapper.lt(Classic::getNum, 2);
        //数据范围 在 1和 3 之间的 ，不包括 1 和 3  想要包括 使用 queryWrapper.le()  queryWrapper.ge()
        //queryWrapper.gt(Classic::getNum, 1);
        //queryWrapper.lt(Classic::getNum, 3);
        //时间具体
        /*Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date  = sdf.parse("2020-09-25 02:00:00");
        }catch (Exception e){
            System.out.println("--------"+ e.getMessage()+"--------");
        }
        queryWrapper.eq(Classic::getCreateDate, date);*/

        // DateTime 格式 会自动 加个.0 导致查询失效
        // queryWrapper.eq(Classic::getCreateDate, "2020-09-25 02:00:00");
        //时间范围
  /*      String start_date = "2020-09-25 02:00:00";
        String end_date = "2020-09-27 04:00:00";
        queryWrapper=  new LambdaQueryWrapper<Classic>()
                .apply(StringUtils.hasText(start_date), "date_format (create_date,'%Y-%m-%d %H:%i:%S') >= date_format('" + start_date + "','%Y-%m-%d %H:%i:%S')")
                .apply(StringUtils.hasText(end_date), "date_format (create_date,'%Y-%m-%d %H:%i:%S') <= date_format('" + end_date + "','%Y-%m-%d %H:%i:%S')")
                .orderByDesc(Classic::getCreateDate);*/

        list = list(queryWrapper);


        return list;
    }
}
